{block name=body}
    <div class="row">
        <table class="table table-bordered table-striped table-hover" id="newsTitleTable">
            <thead>
            <tr>
                <th style="width:50%;text-align:center;line-height:2em;" colspan="4">
                    新闻标签管理
                </th>

            </tr>
            <tr>
                <th style="width:50%;text-align:center;line-height:2em;">标签名</th>
                <th colspan="3" style="text-align:center;line-height:2em;">
                    操作
                    <a class="btn btn-info btn-sm pull-right" id="editNewsBtn" style="margin-left: 10px;">
                        编辑新闻
                    </a>
                    <button class="btn btn-success btn-sm pull-right news-item-title-add">
                        <span class="glyphicon glyphicon-plus"></span>
                        添加
                    </button>
                </th>
            </tr>
            </thead>
            <tbody>
            {foreach $res as $tab}
                <tr data-id="{$tab.id}">
                    <td>
                        <span class="news-item-title-txt" style="font-size:1.5em;">{$tab.title}</span>
                        <input type="text" class="form-control news-item-title-input" value="{$tab.title}" maxlength="7" style="display:none;" />
                    </td>
                    <td>
                        <button class="btn btn-warning btn-sm pull-right news-item-title-edit"><span class="glyphicon glyphicon-pencil"></span> 编辑</button>
                        <button class="btn btn-info btn-sm pull-right news-item-title-edit-save" style="display:none;"><span class="glyphicon glyphicon-floppy-disk"></span> 保存</button>
                    </td>
                    <td>
                        <button class='btn btn-sm btn-danger pull-right news-item-title-del'><span class="glyphicon glyphicon-trash"></span> 删除</button>
                    </td>
                </tr>
            {/foreach}
            </tbody>
        </table>
        <input type="hidden" id="postToken" value="{$token}">
    </div>
    <script type="text/javascript">
        (function(){
            /**
             * 删除
             */
            function newsDelCB(e){
                e.preventDefault();
                var id=$(this).parent().parent().attr('data-id');
                if(id!='temp'){
                    if(confirm("确定要删除吗？")){
                        $.ajax({
                            type : "POST",
                            url : "/admin/includes/processes/processes-news-management.php",
                            cache : false,
                            dataType : "json",
                            data : {
                                type:"delete",
                                id:id
                            },
                            success : function(data) {
                                console.log(data);
                                if (data.type == '1') {
                                    $("#newsTitleTable tr[data-id='"+id+"']").remove();
                                }
                                commonAlertBox(data.content);
                            }
                        });
                    }
                } else {
                    $(this).parent().parent().remove();
                }
            }
            $(".news-item-title-del").on('click',newsDelCB);

            //新闻标题编辑
            function newsEditCB(e){
                e.preventDefault();
                $(this).hide();
                $(this).parent().find('.news-item-title-edit-save').show();
                $(this).parent().parent().find('.news-item-title-txt').hide();
                $(this).parent().parent().find('.news-item-title-input').show();
            }
            $(".news-item-title-edit").on('click',newsEditCB);

            //新闻标题编辑保存
            function newsEditSaveCB(e){
                e.preventDefault();
                $(this).hide();
                $(this).parent().find('.news-item-title-edit').show();
                var $this=this;
                var params={};
                params.id=$(this).parent().parent().attr('data-id');
                params.formToken=$("#postToken").val();
                params.title=$(this).parent().parent().find('.news-item-title-input').val();
                if(params.id!='temp'){
                    params.type='edit';
                }else{
                    params.type='add';
                }
                $.post('/admin/includes/processes/processes-news-management.php',params,function(data){
                    console.log(data);
                    if (data.type == '1') {
                        if(params.type=='edit')
                            $($this).parent().parent().find('.news-item-title-txt').html(params.title);
                        else {
                            window.location.href=window.location.pathname;
                        }
                    } else if(data.type == '0') {
                        commonAlertBox(data.content);
                    }
                    $($this).parent().parent().find('.news-item-title-txt').show();
                    $($this).parent().parent().find('.news-item-title-input').hide();
                },'json');
            }
            $(".news-item-title-edit-save").on('click',newsEditSaveCB);

            //添加新闻标签
            $(".news-item-title-add").on('click',function(e){
                e.preventDefault();
                if($("#newsTitleTable tbody tr[data-id='temp']").length==0){
                    var tbodyHTML=$("#newsTitleTable tbody").html();
                    var addHTML='<tr data-id="temp"><td><span class="news-item-title-txt" style="font-size:1.5em;">点击编辑按钮为新标签命名(注:最大长度为7位)</span>';
                    addHTML+='<input type="text" class="form-control news-item-title-input" value="" style="display:none;" maxlength="7" /></td><td>';
                    addHTML+='<button class="btn btn-warning btn-sm pull-right news-item-title-edit"><span class="glyphicon glyphicon-pencil"></span> 编辑</button>';
                    addHTML+='<button class="btn btn-info btn-sm pull-right news-item-title-edit-save" style="display:none;" ><span class="glyphicon glyphicon-floppy-disk"></span> 保存</button></td><td>';
                    addHTML+='<button class="btn btn-sm btn-danger pull-right news-item-title-del"><span class="glyphicon glyphicon-trash"></span> 删除</button></td></tr>';
                    $("#newsTitleTable tbody").html(tbodyHTML+addHTML);
                    $(".news-item-title-edit-save").on('click',newsEditSaveCB);
                    $(".news-item-title-edit").on('click',newsEditCB);
                    $(".news-item-title-del").on('click',newsDelCB);
                }else{
                    commonAlertBox('列表中已存在"新建标签"');
                }
            });

            //编辑新闻跳转
            $("#editNewsBtn").on('click',function(e){
                e.preventDefault();
                if($("#newsTitleTable tbody>tr").length!=0){
                    window.location.href='/admin/general/newlist';
                }else{
                    commonAlertBox('至少需要一个新闻分类标签');
                }
            });
        })();



    </script>
{/block}
